<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools :is-show-before="false">
        <template v-slot:after>
          <el-button type="primary" size="small" @click="showAdd('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table :data="list" style="margin: 10px 0;" border row-key="id">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="showAdd(row.id,2)">添加</el-button>
            <el-button type="text" @click="showEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="权限点" :visible.sync="showDialog">
      <!-- 表单 -->
      <el-form ref="perForm" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="onSave">确定</el-button>
          <el-button size="small" @click="showDialog = false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermission,
  addPermission,
  delPermission,
  updatePermission,
  getPermissionDetail
} from '@/api/permission.js'
import { tranListToTree } from '@/utils'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: false // 开启
      }
    }
  },

  created() {
    this.loadPermission()
  },

  methods: {
    async loadPermission() {
      const res = await getPermission()
      // console.log(res)
      this.list = tranListToTree(res, '0')
    },
    // 点击添加按钮，显示弹框
    showAdd(pid, type) {
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 点击确定按钮
    async onSave() {
      // 表单验证忽略
      // 发起请求
      // console.log(this.formData)
      // 判断如果this.formData.pid中有pid，则代表是编辑
      if (this.formData.pid) {
        console.log('编辑')
        await updatePermission(this.formData)
        this.$message.success('更新成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }
      this.showDialog = false
      this.loadPermission()
    },
    // 点击编辑
    async showEdit(id) {
      // 根据ID获取权限点详情
      const res = await getPermissionDetail(id)
      // console.log(res)
      this.showDialog = true
      this.formData = res
    },
    // 点击删除
    async delPermission(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.loadPermission()
    }
  }
}
</script>

<style scoped lang='less'>
</style>
